<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2015/12/9
 * Time: 17:16
 */

include_once(ROOTDIR.'include/ap.class.php');
include_once(ROOTDIR.'include/sdk.class.php');

if(empty($_SESSION["userinfo"])){die("<script>top.location.href='index.php'</script>");}

//调用接口类
class Autoreply_con extends ap
{
    //查询消息自动回复
    public function index()
    {
        //用户ID
        $userid = $this->userinfo['userid'];
        //消息自动回复
        $info = $this->viewdata("*","apreply_msg","where userid={$userid}");
        $retype = $info['type'] ? $info['type'] : 2;
        $this->smarty->assign("retype",$retype);
        //回复文字
        if($info) {
            if ($info['type'] == 0)
            {
                $this->smarty->assign("info", $info);
            }
            else
            {
                //回复图文广播
                $msg = $this->arrdata('*', 'apfodder', "where msgid={$info['content']}");
                foreach ($msg as $k => $v) {
                    $msg[$k] = array(
                        'title' => $v['title'],
                        'img_url' => $v['img_url'],
                        'abstract' => $v['abstract'],
                        'content' => $v['content'],
                        'time' => $v['time'],
                    );
                }
                //echo '<pre>';
                //print_r($msg);die;
                $msgnum = count($msg);

                $this->smarty->assign("info", $msg);
                $this->smarty->assign("msgnum", $msgnum);
            }
        }

        //素材列表
        $fodder = $this->arrdata('*','apfodder',"where userid={$userid}");

        $data = array();
        foreach ($fodder as $k => $v)
        {
            $data[$v['msgid']][] = array(
                'id'       => $v['id'],
                'userid'   => $userid,
                'title'    => $v['title'],
                'img_url'  => $v['img_url'],
                'abstract' => $v['abstract'],
                'content'  => $v['content'],
                'time'     => $v['time'],
                'type'     => $v['type'],
            );
        }

        //echo '<pre>';
        //print_r($data);die;

        $this->smarty->assign("basehost",$this->basehost);
        $this->smarty->assign("fodder",$data);
        $this->smarty->display("autoReply.tpl");
    }

    //添加/编辑消息自动回复
    public function msgedit()
    {
        //print_r($_POST);die;
        //用户ID
        $userid = $this->userinfo['userid'];

        //接收参数
        $into = array(
            'userid'   => $userid,
            'content'    => $_POST['content'],
            'type'    => $_POST['type'],
        );

        $ishave = $this->viewdata("*","apreply_msg","where userid={$userid}");

        if(empty($ishave)){
            $res = $this->setdata("apreply_msg",$into);
        }else{
            $res = $this->updata("apreply_msg",$into,"where userid={$userid}");
        }

        if($res)
        {
            $data = array(
                'status'    => 1,
            );
            //提示保存成功
            echo json_encode($data);
        }
    }

    //删除消息自动回复
    public function msgdel()
    {
        //用户ID
        $userid = $this->userinfo['userid'];
        //删除
        $res = $this->deldata('apreply_msg',"where userid={$userid}");

        if($res)
        {
            //提示删除成功、跳转到列表页面
            echo json_encode(array('status' => 1));
        }
    }


}